package com.dks.repository.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dks.dto.AttendanceCourseDto;
import com.dks.dto.CourseSignDto;
import com.dks.dto.TeacherToDoDto;
import com.dks.dto.WaitCourseDto;
import com.dks.dto.student.StudentTodoDto;
import com.dks.mapper.ClassHourPackageMapper;
import com.dks.mapper.CourseCategoryMapper;
import com.dks.mapper.CourseMapper;
import com.dks.mapper.EvaluateMapper;
import com.dks.mapper.FeedbackSheetMapper;
import com.dks.mapper.StudentClassMapper;
import com.dks.mapper.UserClassHourPackageMapper;
import com.dks.models.Course;
import com.dks.models.CourseCategory;
import com.dks.models.student.UserClassHourPackage;
import com.dks.repository.ICourseRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;
import com.dks.util.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class CourseRepository implements ICourseRepository {

    private CourseMapper courseMapper;

    private UserClassHourPackageMapper userClassHourPackageMapper;

    private ClassHourPackageMapper classHourPackageMapper;

    private CourseCategoryMapper courseCategoryMapper;

    private StudentClassMapper studentClassMapper;

    private EvaluateMapper evaluateMapper;

    private FeedbackSheetMapper feedbackSheetMapper;

    @Autowired
    public CourseRepository(final CourseMapper courseMapperIn,
                            final UserClassHourPackageMapper userClassHourPackageMapper,
                            final ClassHourPackageMapper classHourPackageMapper,
                            final CourseCategoryMapper courseCategoryMapper,
                            final StudentClassMapper studentClassMapper,
                            final EvaluateMapper evaluateMapper,
                            final FeedbackSheetMapper feedbackSheetMapper) {
        this.courseMapper = courseMapperIn;
        this.userClassHourPackageMapper = userClassHourPackageMapper;
        this.classHourPackageMapper = classHourPackageMapper;
        this.courseCategoryMapper = courseCategoryMapper;
        this.studentClassMapper = studentClassMapper;
        this.evaluateMapper = evaluateMapper;
        this.feedbackSheetMapper = feedbackSheetMapper;
    }

    @Override
    public Integer insert(Course course) {
        return courseMapper.insert(course);
    }

    @Override
    public Integer update(Course course) {
        return courseMapper.update(course);
    }

    @Override
    public Integer delete(Integer id) {
        return courseMapper.delete(id);
    }

    @Override
    public Course select(Integer id) {
        return courseMapper.select(id);
    }

    @Override
    public Integer cntByMap(Map<String, Object> map) {
        return courseMapper.cntByMap(map);
    }

    @Override
    public List<Integer> getByMap(Map<String, Object> map) {
        return courseMapper.getByMap(map);
    }

    @Override
    public List<Course> getByMapList(Map<String, Object> map) {
        return courseMapper.getByMapList(map);
    }

    @Override
    public Integer updateByCode(Course course) {
        return courseMapper.updateByCode(course);
    }

    @Override
    public Course selectByBfCode(String bfCode) {
        return courseMapper.selectByBfCode(bfCode);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取课程列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override

    public ResultModel<Object> getCourseList(String courseCategory, Integer pageNum, Integer pageSize, String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("courseCategory", courseCategory);
        map.put("nameParam", name);
        map.put("recStatus", 0);
        map.put("sortColumns", "app_sort ASC");
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Course> courseList = courseMapper.getByMapList(map);
        PageInfo<Course> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), courseList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取课程详情
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: courseId 课程id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getCourseInfo(Integer courseId) {
        Course course = courseMapper.select(courseId);
        return Result.makeResult(ResultInfo.SUCCESS, course);
    }

    public ResultModel<PageInfo<Course>> getList(String courseName, String courseCategory, String courseStage, Integer pageNum, Integer pageSize) {
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Course> courseList = courseMapper.getList(courseName, courseCategory, courseStage);
        PageInfo<Course> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), courseList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取是否需要续期提醒
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getRenewalReminder(Integer userId) {

        UserClassHourPackage userClassHourPackage = userClassHourPackageMapper.selectAvailableClassHourCount(Long.valueOf(userId));
        if (null == userClassHourPackage) {
            return Result.makeResult(ResultInfo.SUCCESS, "");
        }
        return Result.makeResult(ResultInfo.SUCCESS, userClassHourPackage.getAvailableClassHourCount());
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取需要签到的课程
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getCourseSignList(Integer parentId, String beginTime, String endTime, Integer pageSize, Integer pageNum) {

        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("parentId", parentId);
        map.put("attendClassStatus", 0);
        map.put("classAttendStatus", "NOT_SIGN_IN");
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<CourseSignDto> courseSignDtolist = courseMapper.getCourseSignList(map);
        PageInfo<CourseSignDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), courseSignDtolist);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }


    /**
     * @Author: Ah Nan
     * @Description: 获取存在代办事项的课程
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getWaitCourseList(Integer parentId) {
        List<WaitCourseDto> waitCourseDtoList = courseMapper.getWaitCourseList(parentId);
        return Result.makeResult(ResultInfo.SUCCESS, waitCourseDtoList);
    }


    /**
     * @Author: Ah Nan
     * @Description: 获取待上课课程列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getAttendCourseList(Integer parentId) {
        List<CourseSignDto> courseSignDtolist = courseMapper.getAttendCourseList(parentId);
        return Result.makeResult(ResultInfo.SUCCESS, courseSignDtolist);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取推荐课程列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getRecommendCourseList(Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("recommendStatus", 0);
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Course> rewardDtoList = courseMapper.getByMapList(map);
        PageInfo<Course> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), rewardDtoList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }


    /**
     * @Author: Ah Nan
     * @Description: 获取当前用户单日课程列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: beginTime 开始时间
     * @Param: endTime 结束时间
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getSingleDayCourseList(String beginTime, String endTime, Integer pageNum, Integer pageSize, Integer parentId, Integer studentId, String courseCategory, Integer ageMin, Integer ageMax) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("parentId", parentId);
        map.put("endTime", endTime);
        map.put("studentId", studentId);
        map.put("courseCategory", courseCategory);
        map.put("studentId", studentId);
        map.put("ageMinParam", ageMin);
        map.put("ageMaxParam", ageMax);
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<CourseSignDto> rewardDtoList = courseMapper.getSingleDayCourseList(map);
        PageInfo<CourseSignDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), rewardDtoList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取当前用户上课记录列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getClassRecordList(Integer pageNum, Integer pageSize, Integer studentId) {
        Map<String, Object> map = new HashMap<>();
        map.put("studentId", studentId);
        map.put("attendClassStatus", 0);
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<CourseSignDto> rewardDtoList = courseMapper.getSingleDayCourseList(map);
        PageInfo<CourseSignDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), rewardDtoList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }


    /**
     * @Author: Ah Nan
     * @Description: 获取教师所上课程分类列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: name 分类名称
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getTeacherCourseCategoryList(Integer teacherId, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("teacherId", teacherId);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<CourseCategory> teacherList = courseCategoryMapper.getTeacherCourseCategoryList(map);
        PageInfo<CourseCategory> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), teacherList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * 获取教师待办事项列表
     *
     * @param pageNum
     * @param pageSize
     * @param teacherId
     * @return
     */
    @Override
    public ResultModel<Object> getTeacherToDoList(Integer pageNum, Integer pageSize, Long teacherId) {
        Map<String, Object> map = new HashMap<>();
        map.put("recStatus", 0);
        map.put("teacherId", teacherId);
        map.put("attendClassStatus", 1);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<TeacherToDoDto> teacherToDoDtos = courseMapper.getTeacherToDoList(map);

        List<TeacherToDoDto> resultList = new ArrayList<>();

        try {
            for (TeacherToDoDto teacherToDoDto : teacherToDoDtos) {
                //上课总人数
                Integer allStudentClassCount = studentClassMapper.countClassAttendNum(teacherToDoDto.getClassId());

                //请假人数
                Integer classLeaveNum = studentClassMapper.countClassLeaveNum(teacherToDoDto.getClassId());

                //课堂反馈已完成数
                Integer actualFeedbackSheetCount = feedbackSheetMapper.countFeedbackSheetNum(teacherToDoDto.getClassId());

                //已批改作业数
                Integer actualTaskCount = studentClassMapper.countTaskCorrectNum(teacherToDoDto.getClassId());

                teacherToDoDto.setAllStudentClassCount(allStudentClassCount);
                teacherToDoDto.setActualStudentClassCount(allStudentClassCount - classLeaveNum);
                teacherToDoDto.setAllFeedbackSheetCount(allStudentClassCount);
                teacherToDoDto.setActualFeedbackSheetCount(actualFeedbackSheetCount);
                teacherToDoDto.setAllTaskCount(allStudentClassCount);
                teacherToDoDto.setActualTaskCount(actualTaskCount);
                resultList.add(teacherToDoDto);
            }
        } catch (Exception e) {
            PageInfo<TeacherToDoDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), resultList);
            return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
        }

        return Result.makeResult(ResultInfo.SUCCESS, resultList);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取课程分类
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: name 分类名称
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getCourseCategory(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("recStatus", 0);
        map.put("sortColumns", "app_sort ASC");
        List<CourseCategory> courseCategoryList = courseCategoryMapper.getByMapList(map);
        return Result.makeResult(ResultInfo.SUCCESS, courseCategoryList);

    }

    /**
     * @Author: Ah Nan
     * @Description: 【首页】获取上课提醒列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getAttendCoursRemindeList(Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<StudentTodoDto> rewardDtoList = courseMapper.getStudentTodoList(map);
        PageInfo<StudentTodoDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), rewardDtoList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }


    /**
     * @Author: Ah Nan
     * @Description: 【首页】获取待办列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: parentId 家长id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getAwitHandleList(Integer pageNum, Integer pageSize, Long userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("recStatus", 0);
        map.put("userId", userId);
        map.put("userId", userId);
        map.put("attendClassStatus", 1);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<StudentTodoDto> studentTodoDtos = courseMapper.getStudentTodoList(map);
        PageInfo<StudentTodoDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), studentTodoDtos);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);

    }

    /**
     * @Author: Ah Nan
     * @Description: 【教务】获取单节课程考勤列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: attendClassStatus 上课状态:0上课,1下课
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getAttendanceCourseList(Integer pageNum, Integer pageSize, Integer classId, String classAttendStatus, String studentName, String parentName) {
        Map<String, Object> map = new HashMap<>();
        map.put("qsClassId", classId);
        map.put("classAttendStatus", classAttendStatus);
        map.put("studentName", studentName);
        map.put("parentName", parentName);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<AttendanceCourseDto> attendanceCourseList = courseMapper.getAttendanceCourseList(map);
        PageInfo<AttendanceCourseDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), attendanceCourseList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }


}
